/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sioc.factory;

import com.jspx.cache.core.CacheEntry;
import com.jspx.cache.JSCacheManager;
import com.jspx.cache.Cache;
import com.jspx.cache.CacheManager;
import com.jspx.cache.store.MemoryStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.utils.BeanUtil;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-2-12
 * Time: 12:12:38
 */
public class LifecycleManager implements Lifecycle {
    final private static Logger log = LoggerFactory.getLogger(LifecycleManager.class);
    public static final String cacheName = "siocCache";
    private static Cache cache = null;

    static {
        CacheManager cacheManager = JSCacheManager.getCacheManager();
        try {
            cache = cacheManager.createCache(new MemoryStore(), cacheName, 30, 100000, false, null);
            cache.registeredEventListeners(new LifecycEventListeners());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("sioc 初始化错误", e);
        }
    }

    public LifecycleManager() {

    }

    private static String getCacheKey(String beanName, final String namespace) {
        return beanName + "_" + namespace;
    }

    public void put(String beanName, final LifecycleObject lifecycleObject) throws Exception {
        if (lifecycleObject == null) return;
        CacheEntry cacheEntry = new CacheEntry();
        cacheEntry.setKey(getCacheKey(beanName, lifecycleObject.getNamespace()));
        cacheEntry.setAccessKeep(true);
        cacheEntry.setValue(lifecycleObject);
        cacheEntry.setEternal(lifecycleObject.isSingleton());
        cacheEntry.setSingle(lifecycleObject.isSingleton());
        cache.put(cacheEntry);

        if (!lifecycleObject.getNamespace().equalsIgnoreCase(lifecycleObject.getRefNamespace())&&cache.get(getCacheKey(beanName, lifecycleObject.getRefNamespace()))==null)
        {  //如果两个相同的，命名空间不同

            cacheEntry = new CacheEntry();
            cacheEntry.setKey(getCacheKey(beanName, lifecycleObject.getRefNamespace()));
            cacheEntry.setAccessKeep(true);
            cacheEntry.setValue(lifecycleObject);
            cacheEntry.setEternal(lifecycleObject.isSingleton());
            cacheEntry.setSingle(lifecycleObject.isSingleton());
            cache.put(cacheEntry);
        }

    }

    public LifecycleObject get(String beanName, final String namespace) {
        CacheEntry cacheEntry = cache.get(getCacheKey(beanName, namespace));
        if (cacheEntry == null) return null;
        return (LifecycleObject) cacheEntry.getValue();
    }

    public void shutdown() {
        for (String key : cache.getKeys()) {
            CacheEntry cacheEntry = cache.get(key);
            LifecycleObject lifecycleObject = (LifecycleObject) cacheEntry.getValue();
            if (lifecycleObject != null && lifecycleObject.getObject() != null) {
                try {
                    Object bean = lifecycleObject.getObject();
                    if (!StringUtil.isNULL(lifecycleObject.getDestroyMethod())&&ClassUtil.haveMethodsName(bean.getClass(), lifecycleObject.getDestroyMethod()))
                    {
                         BeanUtil.invoke(lifecycleObject.getObject(), lifecycleObject.getDestroyMethod());
                    }
                } catch (Exception e) {
                    log.error(key,e);
                }
            }
            cache.remove(key);
        }
        cache.removeAll();
    }

}